---
title: Editing

demonstrates:
- adding basic keyboard navigation and editing
- custom editors and validators
- auto-edit settings

controls:
- '<button onclick="grid.setOptions({autoEdit:true})">Auto-edit ON</button>'
- '<button onclick="grid.setOptions({autoEdit:false})">Auto-edit OFF</button>'

requires_scripts:
- dist/compat/slick.editors.js
- dist/compat/slick.formatters.js

requires_slick_scripts:
- plugins/slick.cellrangedecorator.js
- plugins/slick.cellrangeselector.js
- plugins/slick.cellselectionmodel.js
---

<div id="myGrid"></div>

<script>

    function requiredFieldValidator(value) {
        if (value == null || value == undefined || !value.length) {
            return { valid: false, msg: "This is a required field" };
        } else {
            return { valid: true, msg: null };
        }
    }

    const columns = [
        { field: "title", width: 100, cssClass: "cell-title", editor: Slick.Editors.Text, validator: requiredFieldValidator },
        { field: "description", width: 120, editor: Slick.Editors.LongText },
        { field: "duration", editor: Slick.Editors.Text },
        { field: "percentComplete", name: "% Complete", width: 80, resizable: false, format: Slick.PercentCompleteBarFormatter, editor: Slick.PercentCompleteEditor },
        { field: "start", minWidth: 110, editor: Slick.Editors.Date },
        { field: "finish", minWidth: 110, editor: Slick.Editors.Date },
        { field: "effortDriven", width: 80, minWidth: 20, maxWidth: 80, cssClass: "cell-effort-driven", editor: Slick.CheckboxEditor, format: Slick.CheckBoxFormatter }
    ];

    const data = [];
    for (var i = 0; i < 500; i++) {
        var d = (data[i] = {});
        d["title"] = "Task " + i;
        d["description"] = "This is a sample task description.\n  It can be multiline";
        d["duration"] = "5 days";
        d["percentComplete"] = Math.round(Math.random() * 100);
        d["start"] = "01/01/2009";
        d["finish"] = "01/05/2009";
        d["effortDriven"] = (i % 5 == 0);
    }

    const grid = new Slick.Grid("#myGrid", data, columns, {
        editable: true,
        enableAddRow: true,
        enableCellNavigation: true,
        asyncEditorLoading: false,
        autoEdit: false
    });

    grid.setSelectionModel(new Slick.CellSelectionModel());

    grid.onAddNewRow.subscribe(function (e, args) {
        var item = args.item;
        grid.invalidateRow(data.length);
        data.push(item);
        grid.updateRowCount();
        grid.render();
    });
</script>

<style>
    .cell-title {
        font-weight: bold;
    }

    .cell-effort-driven {
        text-align: center;
    }
</style>
